import linq from 'linq'

class lineService {

	getMaxId() {
		if (this.lines.length > 0) {
			return linq.from(this.lines).max(x => x.id);
		}
		return 1;
	}
	save() {
		// console.log(this.lines);
		uni.setStorageSync('lines', JSON.stringify(this.lines));
	}
	getAll() {
		const json = uni.getStorageSync('lines');
		if (json !== null && json.length > 0) {
			this.lines = JSON.parse(json);
		} else {
			this.lines = [{
					id: 1,
					lineName: "一号线",
					company_points: [],
					home_points: []
				},
				{
					id: 2,
					lineName: "二号线",
					company_points: [],
					home_points: []
				},
				{
					id: 3,
					lineName: "三号线",
					company_points: [],
					home_points: []
				},
				{
					id: 4,
					lineName: "四号线，二环线",
					company_points: [{
							stationName: '立汇美罗湾',
							sort: 1,
							position: [126.69549, 45.696359],
							plantime: '6:30',
							employees: []
						},
						{
							stationName: '三合路加油站',
							sort: 2,
							position: [126.663917, 45.715355],
							plantime: '6:35',
							employees: []
						},
						{
							stationName: '乐园街',
							sort: 3,
							position: [126.654499, 45.718543],
							plantime: '6:37',
							employees: []
						},
						{
							stationName: '农林头道街',
							sort: 4,
							position: [126.646663, 45.722396],
							plantime: '6:40',
							employees: []
						},
						{
							stationName: '大自然',
							sort: 5,
							position: [126.634608, 45.726559],
							plantime: '6:43',
							employees: []
						},
						{
							stationName: '乐买超市',
							sort: 6,
							position: [126.629283, 45.728387],
							plantime: '6:45',
							employees: []
						},
						{
							stationName: '和兴路宝食林',
							sort: 7,
							position: [126.61362, 45.733784],
							plantime: '6:50',
							employees: []
						},
						{
							stationName: '大发加油站',
							sort: 8,
							position: [126.603628, 45.737639],
							plantime: '6:53',
							employees: []
						},
						{
							stationName: '前进路与钢铁街交口',
							sort: 9,
							position: [126.591133, 45.744839],
							plantime: '7:00',
							employees: []
						},
						{
							stationName: '达道街与河鼓街交口',
							sort: 10,
							position: [126.593473, 45.754541],
							plantime: '7:05',
							employees: []
						},
						{
							stationName: '江北前进道口转盘道',
							sort: 11,
							position: [126.551427, 45.806379],
							plantime: '7:20',
							employees: []
						},
						{
							stationName: '公司',
							sort: 12,
							position: [126.501903, 45.791241],
							plantime: '7:45',
							employees: []
						}
					],
					home_points: [{
						stationName: '',
						sort: 0,
						position: [],
					}]
				}
			];

		}
		return this.lines;

	}
	addOrUpdate(line) {
		const old = linq.from(this.lines).singleOrDefault(x => x.id === line.id);
		if (old !== null) {
			old.lineName = line.Name;
			old.company_points = line.company_points;
			old.home_points = line.home_points;

		} else {
			line.id = this.getMaxId();
			this.lines.push(line);
		}
		this.save();
	}
	constructor() {
		this.lines = this.getAll();



		// lines: [{
		// 	id: '4',
		// 	lineName: "四号线，二环线",
		// 	points: []
		// }]

	}
}
export default new lineService();
